package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.entity.ArticleSort;
import com.sky.exception.AccountNotFoundException;
import com.sky.mapper.ArticleSortMapper;
import com.sky.service.ArticleSortService;
import com.sky.vo.ArticleSortVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.channels.AcceptPendingException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ArticleSortServiceImpl implements ArticleSortService {

    @Autowired
    private ArticleSortMapper articleSortMapper;

    /**
     * 文章分类查询全部
     * @return
     */
    public List<ArticleSortVO> selectArticleSort() {
        List<ArticleSortVO> articleSortVOS = articleSortMapper.selectArticleSort();
        return articleSortVOS;
    }

    /**
     * 文章分类根据分类民称添加
     * @param articleSort
     */
    public void insertArticleSort(ArticleSort articleSort) {
        articleSort.setCreateTime(LocalDateTime.now());
        articleSort.setUpdateTime(LocalDateTime.now());
        ArticleSortVO articleSortVO  = articleSortMapper.selectArticleSortBy(articleSort);
        if (Objects.nonNull(articleSortVO)){
            //文章分类已存在
            throw new AccountNotFoundException(MessageConstant.ARTICLE_SORT_ALREADY_EXIST);
        }
            articleSortMapper.insertArticleSort(articleSort);
    }

    /**
     * 文章分类修改
     * @param articleSort
     */
    public void updateArticleSort(ArticleSort articleSort) {
        articleSort.setUpdateTime(LocalDateTime.now());
        articleSortMapper.updateArticleSort(articleSort);
    }

    /**
     * 文章分类删除
     * @param id
     */
    public void deleteArticleSort(Long id) {
        articleSortMapper.deleteArticleSort(id);
    }
}
